package chapter07

object Test15_HighLevelFunction_Reduce {

  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3, 4)
    // 1.归约求和
    println(list.reduce(_ + _))
    println(list.reduceLeft(_ + _))
    println(list.reduceRight(_ + _))

    // 2. 归约案例
    val list2 = List(5, 2, 8, 4)
    println(list2.reduce(_ - _)) //-9
    println(list2.reduceRight(_ - _)) // 5-(2-(8-4)) == 7

    // 3.折叠
    println(list2.fold (19) (_ + _)) // 19+10 + 5 +2 +8 +4
    println(list2.fold (19) (_ - _)) // 19-10 -5 -2 -8 -4
    println(list2.foldRight(19) (_ - _)) // (5-(2-(8-(4-19))))

  }
}
